package cn.cdutetc.library.controller;

import cn.cdutetc.library.common.ApiResponse;
import cn.cdutetc.library.entity.dto.BorrowDTO;
import cn.cdutetc.library.entity.dto.BorrowVO;
import cn.cdutetc.library.service.BorrowService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 借阅管理控制器
 */
@RestController
@RequestMapping("/api/borrows")
@Tag(name = "借阅管理", description = "借阅管理相关接口")
public class BorrowController {

    @Autowired
    private BorrowService borrowService;

    @PostMapping
    @Operation(summary = "借书", description = "创建借阅记录")
    public ApiResponse<Integer> borrowBook(@RequestBody @Validated BorrowDTO borrowDTO) {
        Integer borrowId = borrowService.borrowBook(borrowDTO);
        return ApiResponse.success(borrowId);
    }

    @PostMapping("/{borrowId}/return")
    @Operation(summary = "还书", description = "归还图书")
    public ApiResponse<Boolean> returnBook(@PathVariable Integer borrowId) {
        boolean result = borrowService.returnBook(borrowId);
        return ApiResponse.success(result);
    }

    @PostMapping("/{borrowId}/renew")
    @Operation(summary = "续借", description = "续借图书")
    public ApiResponse<Boolean> renewBook(
            @PathVariable Integer borrowId,
            @Parameter(description = "续借天数") @RequestParam(value = "days", required = false) Integer days) {
        // 如果days参数为null，设置默认值为15天
        if (days == null) {
            days = 15;
        }
        boolean result = borrowService.renewBook(borrowId, days);
        return ApiResponse.success(result);
    }

    @PostMapping("/{borrowId}/lost")
    @Operation(summary = "报告丢失", description = "报告图书丢失")
    public ApiResponse<Boolean> reportLost(@PathVariable Integer borrowId) {
        boolean result = borrowService.reportLost(borrowId);
        return ApiResponse.success(result);
    }

    @PostMapping("/{borrowId}/overdue")
    @Operation(summary = "标记逾期", description = "手动标记图书为逾期状态")
    public ApiResponse<Boolean> markOverdue(@PathVariable Integer borrowId) {
        boolean result = borrowService.markOverdue(borrowId);
        return ApiResponse.success(result);
    }

    @PostMapping("/{borrowId}/pay-fine")
    @Operation(summary = "支付罚款", description = "支付借阅罚款")
    public ApiResponse<Boolean> payFine(@PathVariable Integer borrowId) {
        boolean result = borrowService.payFine(borrowId);
        return ApiResponse.success(result);
    }

    @GetMapping("/{borrowId}/fine-amount")
    @Operation(summary = "获取罚款金额", description = "获取丢失图书的罚款金额")
    public ApiResponse<Double> getFineAmount(@PathVariable Integer borrowId) {
        Double amount = borrowService.getFineAmount(borrowId);
        return ApiResponse.success(amount);
    }

    @GetMapping("/{borrowId}")
    @Operation(summary = "获取借阅详情", description = "根据ID获取借阅详情")
    public ApiResponse<BorrowVO> getBorrowById(@PathVariable Integer borrowId) {
        BorrowVO borrowVO = borrowService.getBorrowById(borrowId);
        return ApiResponse.success(borrowVO);
    }

    @GetMapping("/page")
    @Operation(summary = "分页查询借阅记录", description = "分页查询借阅记录")
    public ApiResponse<IPage<BorrowVO>> pageBorrows(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer page,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "10") Integer size,
            @Parameter(description = "用户ID") @RequestParam(required = false) Integer userId,
            @Parameter(description = "借阅状态(borrowing/returned/overdue/lost 或 1/2/3/4)") @RequestParam(required = false) String status,
            @Parameter(description = "关键字") @RequestParam(required = false) String keyword) {
        IPage<BorrowVO> result = borrowService.pageBorrows(page, size, userId, status, keyword);
        return ApiResponse.success(result);
    }

    @GetMapping("/current")
    @Operation(summary = "获取用户当前借阅", description = "获取用户当前借阅记录")
    public ApiResponse<IPage<BorrowVO>> getCurrentBorrows(
            @Parameter(description = "用户ID") @RequestParam Integer userId,
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer page,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "10") Integer size) {
        IPage<BorrowVO> result = borrowService.getCurrentBorrows(userId, page, size);
        return ApiResponse.success(result);
    }

    @GetMapping("/overdue")
    @Operation(summary = "获取逾期借阅", description = "获取逾期借阅记录")
    public ApiResponse<IPage<BorrowVO>> getOverdueBorrows(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer page,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "10") Integer size) {
        IPage<BorrowVO> result = borrowService.getOverdueBorrows(page, size);
        return ApiResponse.success(result);
    }
}